#ifndef BOARD_UART_H
#define BOARD_UART_H 1

#include "stm32f10x.h"
#include "board_timer.h"
#include "board_NVIC.h"
#include "user_CRC.h"
#include "user_pid.h"

#include <stdio.h>

/**
 * @brief Definition for COM port1, connected to USART1
 */
#define BOARD_COM1                        USART1
#define BOARD_COM1_CLK                    RCC_APB2Periph_USART1
#define BOARD_COM1_TX_PIN                 GPIO_Pin_9
#define BOARD_COM1_TX_GPIO_PORT           GPIOA
#define BOARD_COM1_TX_GPIO_CLK            RCC_APB2Periph_GPIOA
#define BOARD_COM1_RX_PIN                 GPIO_Pin_10
#define BOARD_COM1_RX_GPIO_PORT           GPIOA
#define BOARD_COM1_RX_GPIO_CLK            RCC_APB2Periph_GPIOA
#define BOARD_COM1_IRQn                   USART1_IRQn

/**
 * @brief Definition for COM port2, connected to USART2
 */
#define BOARD_COM2                        USART2
#define BOARD_COM2_CLK                    RCC_APB1Periph_USART2
#define BOARD_COM2_TX_PIN                 GPIO_Pin_2
#define BOARD_COM2_TX_GPIO_PORT           GPIOA
#define BOARD_COM2_TX_GPIO_CLK            RCC_APB2Periph_GPIOA
#define BOARD_COM2_RX_PIN                 GPIO_Pin_3
#define BOARD_COM2_RX_GPIO_PORT           GPIOA
#define BOARD_COM2_RX_GPIO_CLK            RCC_APB2Periph_GPIOA
#define BOARD_COM2_IRQn                   USART2_IRQn

/**
 * @brief Definition for COM port3, connected to USART3
 */
#define BOARD_COM3                        USART3
#define BOARD_COM3_CLK                    RCC_APB1Periph_USART3
#define BOARD_COM3_TX_PIN                 GPIO_Pin_10
#define BOARD_COM3_TX_GPIO_PORT           GPIOB
#define BOARD_COM3_TX_GPIO_CLK            RCC_APB2Periph_GPIOB
#define BOARD_COM3_RX_PIN                 GPIO_Pin_11
#define BOARD_COM3_RX_GPIO_PORT           GPIOB
#define BOARD_COM3_RX_GPIO_CLK            RCC_APB2Periph_GPIOB
#define BOARD_COM3_IRQn                   USART3_IRQn

/* Amount of enabled ports. */
#define COMn                             3
 typedef enum
{
  COM1 = 0,
  COM2 = 1,
  COM3 = 2
} COM_TypeDef;

/* Same size for all UARTs */
#define UART_RX_BUFFER_SIZE    64
/* We will implement custom size for all each UART.*/
#define UART1_RX_BUFFER_SIZE    64
#define UART2_RX_BUFFER_SIZE    64
#define UART3_RX_BUFFER_SIZE    64

/* Before use printf we need initialise suitable com port. */
#define TERMINAL_OUTPUT COM1
#define COM1_BAUD_RATE  115200

#if 0
  #define USART1_PRIORITY_GROUP 3
  #define USART1_SUB_PRIORITY   0

  #define USART2_PRIORITY_GROUP 3
  #define USART2_SUB_PRIORITY   1

  #define USART3_PRIORITY_GROUP 3
  #define USART3_SUB_PRIORITY   2
#endif


void uart_init(void);
void board_com_init(COM_TypeDef COM, USART_InitTypeDef* USART_InitStruct);

char read_char_UARTx (COM_TypeDef COMx);
void UARTx_rx_isr(COM_TypeDef COMx, uint8_t ch);
int UARTx_Read_Byte_From_Round_Buffer(COM_TypeDef COMx, uint8_t *ui8_ch, uint16_t timeout);
int ReceiveDataPacket(uint8_t *buffer, uint16_t length, COM_TypeDef COMx);

int fputc(int ch, FILE *f);
uint8_t board_fputc(uint8_t ch);


/* Remove after test.*/
char read_char_UART1 (void);
void UART1_rx_isr(void) ;


/********************/




#endif